﻿using RimWorld;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using UnityEngine;
using Verse;

namespace RimZombieGirl
{
    public class CompAbilityEffect_ToggleHediff : CompAbilityEffect, IAbilityGizmoProvider
    {
        public CompProperties_AbilityToggleHediff Props => (CompProperties_AbilityToggleHediff)props;

        public override void Apply(LocalTargetInfo target, LocalTargetInfo dest)
        {
            // 这个ability不用施法
        }

        public IEnumerable<Command> GetGizmos(Ability ability)
        {
            var pawn = ability.pawn;
            if (pawn == null || pawn.DestroyedOrNull()) yield break;
            if (Props?.hediffDef == null) yield break;

            // 激活：是否有该 Hediff
            bool HasHediff() => pawn.health?.hediffSet?.HasHediff(Props.hediffDef) == true;

            yield return new Command_Toggle
            {
                defaultLabel = !string.IsNullOrEmpty(Props.label) ? Props.label : (ability.def?.label ?? "Toggle Hediff"),
                defaultDesc = !string.IsNullOrEmpty(Props.description) ? Props.description : "Toggle whether this pawn has the hediff.",
                icon = !string.IsNullOrEmpty(Props.iconPath)
                                ? ContentFinder<Texture2D>.Get(Props.iconPath)
                                : (ability.def?.uiIcon ?? BaseContent.BadTex),
                isActive = () => HasHediff(),
                toggleAction = () =>
                {
                    if (HasHediff())
                    {
                        var h = pawn.health.hediffSet.GetFirstHediffOfDef(Props.hediffDef);
                        if (h != null) pawn.health.RemoveHediff(h);
                    }
                    else
                    {
                        var h = HediffMaker.MakeHediff(Props.hediffDef, pawn);
                        if (Props.severity > 0f) h.Severity = Props.severity;
                        pawn.health.AddHediff(h);
                    }
                },
            };
        }
    }
}
